package com.zy.ext;

import com.zy.ext.hanler.MapperHandler;
import com.zy.ext.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;


import java.io.IOException;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Proxy;
import java.util.Set;

/**
 * 存储绑定关系
 * mapper接口和语句的绑定关系  key 接口全限定名称 value mappedStatement
 *
 * @author zy
 * @see org.apache.ibatis.session.Configuration#mappedStatements
 */
@Slf4j
public class T2 {
  public static void main(String[] args) throws IOException {
    Reader resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
    //将扫描的Mapper 放入Configuration MapperRegistry中
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);

    SqlSession sqlSession = sqlSessionFactory.openSession();

    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    Set<?> userList = userMapper.selectList(1);
    Set<?> userList2 = userMapper.selectList(1);
    log.error("userList :{}", userList);
    System.out.println("userList = " + userList);


    MethodHandles.Lookup lookup = MethodHandles.lookup();
    int lookupModes = lookup.lookupModes();
    log.error("lookupModes :{}", lookupModes);
    Class<?> lookupClass = lookup.lookupClass();
    System.out.println("lookupClass = " + lookupClass);
  }

  @Test
  public void contextLoads1() throws NoSuchMethodException {

    Object proxyInstance = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{UserMapper.class}, new MapperHandler(UserMapper.class));
    UserMapper userMapper = (UserMapper) proxyInstance;
    Set<?> selectList = userMapper.selectList(1);
    System.out.println("selectList = " + selectList);

  }
}
